package com.engine4cloud.tdf.education.convert;

import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.education.dto.EduResourceFileDTO;
import com.engine4cloud.tdf.education.dto.query.EduResourceFileQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduResourceFileResultDTO;
import com.engine4cloud.tdf.education.entity.EduResourceFileEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;


@Mapper
public interface EduResourceFileConvert {
    EduResourceFileConvert INSTANCE = Mappers.getMapper(EduResourceFileConvert.class);

    @Mapping(target = "id", ignore = true)
    EduResourceFileEntity createTo(EduResourceFileDTO s);

    EduResourceFileResultDTO convert(EduResourceFileEntity s);

    default EduResourceFileEntity create(String resourceCode, EduResourceFileDTO s, TdfUser user) {
        EduResourceFileEntity entity = createTo(s);
        entity.setResourceCode(resourceCode);
        entity.setCode(CodeUtils.entrustCode());
        entity.setTenantId(user.getTenantId());
        return entity;
    }

    default List<EduResourceFileEntity> createList(EduResourceFileQueryDTO dto, List<EduResourceFileDTO> fileList, TdfUser user) {
        return fileList.stream().map(file -> EduResourceFileConvert.INSTANCE.create(dto.getResourceCode(), file, user)).toList();
    }
}
